package cn.crm.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.crm.bean.Customer;
import cn.crm.bean.Product;
import cn.crm.dao.ProductDao;

@Repository("productDao")
public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao {
	@Override
	public List<Product> getAll() {
		List<Product> list=super.getHibernateTemplate().find("from Product where pdIsdelete=?",false);
		return list.size()>0?list:null;
	}

	@Override
	public Product getProductById(String id) {
		List<Product> list=super.getHibernateTemplate().find("from Product where pdProductid=?",id);
		return list.size()>0?list.get(0):null;
	}

	@Override
	public Object save(Product Product) {
		return super.getHibernateTemplate().save(Product);
	}

	@Override
	public void update(Product Product) {
		super.getHibernateTemplate().update(Product);

	}

	@Override
	public void delete(Product Product) {
		super.getHibernateTemplate().delete(Product);
	}

	
	@Override
	public List<Product> getAll(Integer index, Integer count,Object...params) {
		DetachedCriteria criteria=DetachedCriteria.forClass(Product.class);
		criteria.add(Restrictions.eq("pdIsdelete", false));
		if (params!=null) {
			for (Object res : params) {
				if (res!=null) {
					System.out.println("---------------------------------------------");
					System.out.println(res);
					criteria.add((Criterion)res);
				}
				
			}
		}
		return super.getHibernateTemplate().findByCriteria(criteria, index, count);
	}
	
	@Override
	public List<Product> getAll(Object...params) {
		DetachedCriteria criteria=DetachedCriteria.forClass(Product.class);
		criteria.add(Restrictions.eq("pdIsdelete", false));
		if (params!=null) {
			for (Object res : params) {
				if (res!=null) {
					criteria.add((Criterion)res);
				}
				
			}
		}
		return super.getHibernateTemplate().findByCriteria(criteria);
	}

	@Override
	public Object getCount(String hql) {
		Session session =super.getSession();
		Object count;
		Query query=session.createQuery(hql);
		count=query.uniqueResult();
		return count;
	}
	
	@Override
	public Object getCount(Object... params) {
		Session session =super.getSession();
		
		Criteria cri=session.createCriteria(Product.class);
		cri.setProjection(Projections.rowCount());
		if (params!=null) {
			for (Object res : params) {
				if (res!=null) {
					System.out.println(res);
					cri.add((Criterion)res);
				}
			}
		}
		List list=cri.list();
		return list.size()>0?list.get(0):0;
	}

	@Override
	public List<Product> getAll(String str) {
		String []strs=str.split(",");
		DetachedCriteria criteria=DetachedCriteria.forClass(Product.class);
		criteria.add(Restrictions.eq("pdIsdelete", false));
		criteria.add(Restrictions.in("pdProductid", strs));
		return super.getHibernateTemplate().findByCriteria(criteria);
	}
}
